using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Emlyn.Website.Articles;
using Emlyn.Website.ArticlesPresentation;

public partial class Secure_EditArticles : EmlynPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string lArticleCode = null;

        if (Request.QueryString["code"] != null)
            lArticleCode = Request.QueryString["code"];

        //
        // ArticleList details
        //

        ArticleQuery laq = new ArticleQuery();
        Article larticle = null;

        if (!String.IsNullOrEmpty(lArticleCode))
        {
            larticle = laq.GetArticleByArticleCode(lArticleCode);
        }

        if (larticle == null)
        {
            PresentArticleTitleLinkFactory lpresentArticleTitleLinkFactory
                = new PresentArticleTitleLinkFactory();
            lpresentArticleTitleLinkFactory.aNavigateUrl
                    = Page.Request.Url.AbsolutePath + @"?code=%ArticleCode%";
            lpresentArticleTitleLinkFactory.aEditable = true;

            PresentArticleList lpresentArticleList =
                new PresentArticleList();
            lpresentArticleList.aPresentArticleFactory
                = lpresentArticleTitleLinkFactory;
            lpresentArticleList.aArticleList
                = laq.GetArticlesWithNoParent();

            //ArticlesPresentation_TopLevelArticles ltopLevelPres
            //    = new ArticlesPresentation_TopLevelArticles();

            //llit = new Literal();
            //llit.Text = ltopLevelPres.GetHTMLForArticleLists(
            //    "EditMusic",
            //    "Top Level Article Lists",
            //    new Uri(
            //        new Uri("http://dummy.com/"),
            //        new Uri(Page.Request.Path, UriKind.Relative)
            //        )
            //);

            //pnlBody.Controls.Add(llit);

            pnlBody.Controls.Add(lpresentArticleList);

            Button lNewArticleButton = new Button();
            lNewArticleButton.Text = "Create New Article";
            lNewArticleButton.Click += new EventHandler(NewArticleClick);
            lNewArticleButton.OnClientClick = "return confirm('Create a new article?');";
            lNewArticleButton.CssClass = pnlBody.CssClass;

            pnlBody.Controls.Add(lNewArticleButton);
        }
        else
        {
            //            xArticleListQuery larticleListQuery = new xArticleListQuery();
            //            xArticleList larticleList = larticleListQuery.GetArticleListByArticleListCode(lArticleListCode);

            HyperLink lhref = new HyperLink();
            lhref.Text = "Back to Top Level";
            lhref.NavigateUrl = Page.Request.Url.AbsolutePath;
            pnlBodyTitle.Controls.Add(lhref);

            //
            // ArticleList details
            //
            PresentArticleDetail lpad = new PresentArticleDetail();
            lpad.CssClass = "EditArticle";
            lpad.Editable = true;
            lpad.aArticle = larticle;
            pnlBodyTitle.Controls.Add(lpad);

            //PresentArticleListChildArticles lpalca = new PresentArticleListChildArticles();
            //lpalca.aArticleList = larticleList;
            //lpalca.Editable = true;
            //lpalca.aArticleType = RelationshipType.ChildArticles;
            //lpalca.CssClass = "EditArticlesChild";
            //lpalca.NavigateUrlPrefix = Page.Request.Url.AbsolutePath + "?code=";
            //lpalca.Text = "Children: ";
            //pnlRight.Controls.Add(lpalca);

            Panel lpnl;

            lpnl = new Panel();
            lpnl.CssClass = "EditArticlesParent";
            pnlRight.Controls.Add(lpnl);

            PresentArticleRelatedArticles lpresentArticleRelatedArticles
                = new PresentArticleRelatedArticles();
            lpresentArticleRelatedArticles.aArticle = larticle;
            lpresentArticleRelatedArticles.aArticleType = RelationshipType.ParentArticles;
            lpresentArticleRelatedArticles.Title = "Parents: ";
            lpresentArticleRelatedArticles.CssClass = "EditArticlesParent2";
            lpresentArticleRelatedArticles.NavigateUrl
                = Page.Request.Url.AbsolutePath + @"?code=%ArticleCode%";
            lpresentArticleRelatedArticles.Editable = true;
            lpnl.Controls.Add(lpresentArticleRelatedArticles);

            lpnl = new Panel();
            lpnl.CssClass = "EditArticlesChild";
            pnlRight.Controls.Add(lpnl);

            lpresentArticleRelatedArticles
                = new PresentArticleRelatedArticles();
            lpresentArticleRelatedArticles.aArticle = larticle;
            lpresentArticleRelatedArticles.aArticleType = RelationshipType.ChildArticles;
            lpresentArticleRelatedArticles.Title = "Children: ";
            lpresentArticleRelatedArticles.CssClass = "EditArticlesChild2";
            lpresentArticleRelatedArticles.NavigateUrl
                = Page.Request.Url.AbsolutePath + @"?code=%ArticleCode%";
            lpresentArticleRelatedArticles.Editable = true;
            lpnl.Controls.Add(lpresentArticleRelatedArticles);


            //PresentArticleTitleLinkFactory lpresentArticleTitleLinkFactory;
            //PresentArticleList lpresentArticleList;

            //lpresentArticleTitleLinkFactory = new PresentArticleTitleLinkFactory();
            //lpresentArticleTitleLinkFactory.aNavigateUrl
            //    = Page.Request.Url.AbsolutePath  + @"?code=%ArticleCode%";
            //lpresentArticleTitleLinkFactory.aEditable = true;

            //lpresentArticleList = new PresentArticleList();
            //lpresentArticleList.aPresentArticleFactory
            //    = lpresentArticleTitleLinkFactory;
            //lpresentArticleList.aArticleList
            //    = laq.GetArticlesForChildArticleID(larticle.ArticleID);
            //lpresentArticleList.CssClass = "EditArticlesParent";
            //lpresentArticleList.Title = "Parents: ";

            //pnlRight.Controls.Add(lpresentArticleList);

            //lpresentArticleTitleLinkFactory = new PresentArticleTitleLinkFactory();
            //lpresentArticleTitleLinkFactory.aNavigateUrl
            //    = Page.Request.Url.AbsolutePath + @"?code=%ArticleCode%";
            //lpresentArticleTitleLinkFactory.aEditable = true;

            //lpresentArticleList = new PresentArticleList();
            //lpresentArticleList.aPresentArticleFactory
            //    = lpresentArticleTitleLinkFactory;
            //lpresentArticleList.aArticleList
            //    = laq.GetArticlesForParentArticleID(larticle.ArticleID);
            //lpresentArticleList.CssClass = "EditArticlesChildren";
            //lpresentArticleList.Title = "Children: ";

            //pnlRight.Controls.Add(lpresentArticleList);
        }
    }
    protected void NewArticleClick(object sender, EventArgs e)
    {
        Article larticle = new Article();

        larticle.ArticleName = "New Article " + larticle.ArticleID.ToString();
        larticle.ArticleContent = String.Empty;

        (new ArticleMod()).Save(larticle);

        Page.Response.Redirect(Page.Request.Url.ToString());
    }
}
